package com.dxl.service.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dxl.common.cache.CacheClient;
import com.dxl.common.model.Order;
import com.dxl.common.model.Page;
import com.dxl.common.utils.Utils;
import com.dxl.dao.common.BaseDao;
import com.dxl.entity.common.BaseEntity;

@Service
@Transactional
public class BaseService {
	private static final Logger LOG = Logger.getLogger(BaseService.class);
	
	@Autowired
	protected CacheClient cache;
	@Autowired
	@Qualifier("baseDao")
	protected BaseDao baseDao;
	
	public <E extends BaseEntity> E get(Class<E> type, String id) {
		return baseDao.get(type, id);
	}
	
	public void save(BaseEntity e) {
		baseDao.save(e);
	}
		
	public void insert(BaseEntity e) {
		baseDao.insert(e);
	}
	
	public int update(BaseEntity e) {
		return baseDao.update(e);
	}
	
	public <E extends BaseEntity> int update(Class<E> type, String id, Map<String, Object> params) {
		return baseDao.update(type, id, params);
	}
	
	public <E extends BaseEntity> int update(Class<E> type, String[] ids, Map<String, Object> params) {
		return baseDao.update(type, ids, params);
	}
	
	public <E extends BaseEntity> int update(Class<E> type, Map<String, Object> params, Map<String, Object> values) {
		return baseDao.update(type, params, values);
	}
	
	public <E extends BaseEntity> List<E> list(Class<E> type, Order... orders) {
		return baseDao.list(type, orders);
	}

	public <E extends BaseEntity> Page<E> list(Class<E> type, Integer pageNo, Integer pageSize, Order... orders) {
		return baseDao.list(type, pageNo, pageSize, orders);
	}
	
	public <E extends BaseEntity> Page<E> list(Class<E> type, String[] fieldNames, Integer pageNo, Integer pageSize, Order... orders) {
		return baseDao.list(type, fieldNames, pageNo, pageSize, orders);
	}
	
	public <E extends BaseEntity> Page<E> list(Class<E> type, Map<String, Object> params, Integer pageNo, Integer pageSize, Order... orders) {
		return baseDao.list(type, params, pageNo, pageSize, orders);
	}
	
	public <E extends BaseEntity> Page<E> list(Class<E> type, String[] fieldNames, Map<String, Object> params, Integer pageNo, Integer pageSize, Order... orders) {
		return baseDao.list(type, fieldNames, params, pageNo, pageSize, orders);
	}
	
	public <E extends BaseEntity> List<Map<String, Object>> list (Class<E> type, String[] fieldNames, Map<String, Object> params, Order... orders) {
		return baseDao.list(type, fieldNames, params, orders);
	}
	
	public <E extends BaseEntity> List<E> listByProp(Class<E> type, String prop, Object value, Order... orders) {
		return baseDao.listByProp(type, prop, value, orders);
	}
	
	public <E extends BaseEntity> List<E> listByProps(Class<E> type, Map<String, Object> params, Order... orders) {
		return baseDao.listByProps(type, params, orders);
	}
	
	public <T extends Object, E extends BaseEntity> List<T> listProp(Class<E> type, String prop, Class<T> propType, Map<String, Object> params, Order... orders) {
		return baseDao.listProp(type, prop, propType, params, orders);
	}
	
	public <E extends BaseEntity> int del(Class<E> type) {
		return baseDao.del(type);
	}
	
	public <E extends BaseEntity> int del(Class<E> type, String id) {
		return baseDao.del(type, id);
	}
	
	public <E extends BaseEntity> int del(Class<E> type, String... ids) {
		return baseDao.del(type, ids);
	}
	
	public <E extends BaseEntity> int del(Class<E> type, Map<String, Object> params) {
		return baseDao.del(type, params);
	}
	
	public <E extends BaseEntity> int count(Class<E> type) {
		return baseDao.count(type);
	}
	
	public <E extends BaseEntity> int count(Class<E> type, Map<String, Object> params) {
		return baseDao.count(type, params);
	}
	
	public <E extends BaseEntity> void state(Class<E> type, String[] ids, Integer state) {
		if (Utils.isEmpty(ids)) {
			return ;
		} else {
			Map<String, Object> values = new HashMap<String, Object>();
			values.put("state", state);
			this.update(type, ids, values);
		}
	}
}
